<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>
<script>
import {
	hasEditor,
	getEditorTitle,
	buildFormData,
	formValidation,
	changeEditor,
	autoChangeEditor,
	configSelectData,
	formShowLoading,
	configFormEditor,
	getWorkCacheKey,
	setSubmitSubValue,
	changeConfigAttr,
	formModelLoad,
	getFormFields
} from '@/static/js/formUtil.js';
import { addF } from '@/static/js/common.js';
import dateTimePicker from '@/static/js/dateTimePicker.js';
import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return buildFormData({
			userCacheKey: 'mkt-customer-requirement-user-cache'
		});
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	methods: {
		// selectZpos(item) {
		// 	let _that = this;
		// 	let autoform = _that.$refs.autoform;
		// 	let zposId = item.id;
		// 	let zuserId = _that.userId;
		// 	if (autoform) {
		// 		//业务人员为空
		// 		if (!autoform.formdata.userPostId) {
		// 			//制表人、制表岗位不为空
		// 			if (zposId && zuserId) {
		// 				autoform.om
		// 					.request('orgInstPosUserOfficial/findListByOrg', {
		// 						posCanBeNull: false,
		// 						joinUserId: zuserId,
		// 						joinPostId: zposId
		// 					})
		// 					.then(res => {
		// 						if (res && res.length > 0) {
		// 							var data = res[0];
		// 							_that.loadDataByUserPost(data);
		// 						}
		// 					});
		// 			} else {
		// 				that.clearUserPost();
		// 			}
		// 		}
		// 	}
		// },
		loadDataByUserPost(d) {
			let formdata = this.$refs.autoform.formdata;
			formdata.userPostId = d.id;
			formdata.userId = d.userId;
			formdata.userName = d.userName;
			formdata.postId = d.posId;
			formdata.postName = d.posName;
			formdata.instId = d.instId;
			formdata.instName = d.instName;
			formdata.mobile = d.autoMobile;
		},
		clearUserPost() {
			let _that = this;
			_that.$refs.autoform.clear('title1', 'userPostId');
			_that.$refs.autoform.clear('title1', 'postId');
			_that.$refs.autoform.clear('title1', 'instId');
			_that.$refs.autoform.clear('title1', 'mobile');
		},

		//计算合计值
		costSumVal(rowsList) {
			var expectTotalPriceNum = '';
			var totalPriceNum = '';
			for (var i in rowsList) {
				if (rowsList[i].expectTotalPriceNum) {
					expectTotalPriceNum = addF(expectTotalPriceNum, rowsList[i].expectTotalPriceNum);
				}
				if (rowsList[i].totalPriceNum) {
					totalPriceNum = addF(totalPriceNum, rowsList[i].totalPriceNum);
				}
			}
			return { expectTotalPriceNum: expectTotalPriceNum, totalPriceNum: totalPriceNum };
		},

		formSubmit() {
			let _that = this;
			var checkRes = formValidation(this.$refs.autoform.configData, this.formdata);
			if (checkRes) {
				this.om.toast(checkRes);
			} else {
				//子表提交数据赋值
				setSubmitSubValue();
				if (!_that.formdata.productStr) {
					this.om.toast(this.subEditorObj['title3'].title + '不能为空');
					return;
				}
				if(_that.edit){
					_that.om.formSave('customerRequirement/doUpdate', _that.formdata, _that);
				}else{
					_that.om.formSave('customerRequirement/doAdd', _that.formdata, _that);
				}	
			}
		},
		initLoad(options) {
			let fileId = options.fileId;
			let _that = this;
			var ajaxArr = [
				urlUtil.zposUrl(options.code),
				urlUtil.dictZeroComboboxUrl('37658'), //重要程度
				urlUtil.sysdictComboboxUrl('38525'), //订货类别
				urlUtil.sysdictComboboxUrl('38526'), //营销渠道
				urlUtil.sysdictComboboxUrl('38527') //运输方式
			];
			if(fileId){
				ajaxArr.push({
					url:"customerRequirement/getValue",
					param:{fileId:fileId}
				})
			}
			
			formModelLoad(
				ajaxArr,
				function(res, mainData) {
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'), mainData);

					//业务人员
					changeEditor(
						'title1',
						'userPostId',
						{
							link: '../../public/pageSearch/pageSearch',
							linkParam: {
								other: JSON.stringify({
									firstUserId: _that.userId
								}),
								namefield: urlUtil.officePageUrl().namefield,
								searchfield: 'q',
								url: urlUtil.officePageUrl().url,
								cachename: _that.userCacheKey,
								placeholder: urlUtil.officePageUrl().prompt
							}
						},
						null,
						mainData
					);

					//重要程度
					changeEditor('title2', 'importanceId', 'data', configSelectData(res[1], 'id', 'dicname'), mainData);
					//订货类别
					changeEditor('title2', 'orderTypeId', 'data', configSelectData(res[2], 'id', 'dicname'), mainData);
					//营销渠道
					changeEditor('title2', 'saleTypeId', 'data', configSelectData(res[3], 'id', 'dicname'), mainData);
					//运输方式
					changeEditor('title2', 'trafficTypeId', 'data', configSelectData(res[4], 'id', 'dicname'), mainData);

					//产品信息
					if (_that.subConfigData['title3']) {
						var configData = _that.subConfigData['title3']
						var attr = {}
						if(hasEditor('title3','projectName',configData)){
							attr.titleTemplate = '<%= projectName %>'
						}
						if(hasEditor('title3','projectNum',configData)){
							attr.subtitle1Template = '<%= projectNum %>'
						}
						if(hasEditor('title3','projectUnit',configData)){
							attr.subtitle2Template = '<%= projectUnit %>'
						}
						if(hasEditor('title3','expectTotalPriceNum',configData)){
							attr.subtitle2Template = '<%= expectTotalPriceNum %>'
						}
						if(hasEditor('title3','projectDeliveryDate',configData)){
							attr.subtitle2Template = '<%= projectDeliveryDate %>'
						}
						changeEditor('title3', 'productStr', attr, mainData);
					}
					changeEditor(
						'title4_1',
						'checkerStr',
						{
							// title: _that.subEditorObj['title5'].title.substr(1),
							titleTemplate: '<%= userName %>',
							subtitle1Template: '<%= postName %>'
						},
						null,
						mainData
					);
					changeEditor(
						'title5',
						'reviewerStr',
						{
							// title: _that.subEditorObj['title5'].title.substr(1),
							titleTemplate: '<%= userName %>',
							subtitle1Template: '<%= postName %>'
						},
						null,
						mainData
					);
					//赋值
					if (fileId) {
						var objData = res[5].value
						var fieldArr = getFormFields(mainData);
						fieldArr.push('id');
						var productList = _that._.cloneDeep(objData.productList);
						_that.subEditorObj['title3'].workList = productList;
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'productStr', JSON.stringify(productList));
						_that.$set(_that.formdata, 'productStr', JSON.stringify(productList));
						delete objData.productList;
						for (var i in objData) {
							if (fieldArr.includes(i)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, objData[i]);
								_that.$set(_that.formdata, i, objData[i]);
							}
						}
						if (_that.subEditorObj['title3']) {
							//配置子表的新增和删除
							changeEditor(
								'title3',
								'productStr',
								{
									showDel: !_that.checkedit,
									showAdd: !_that.checkedit
								},
								null,
								mainData
							);
						}
						
						var checkerList = _that._.cloneDeep(objData.checkerList);
						_that.subEditorObj['title4_1'].workList = checkerList;
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'checkerStr', JSON.stringify(checkerList));
						_that.$set(_that.formdata, 'checkerStr', JSON.stringify(checkerList));
						delete objData.checkerList;
						for (var i in objData) {
							if (fieldArr.includes(i)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, objData[i]);
								_that.$set(_that.formdata, i, objData[i]);
							}
						}
						if (_that.subEditorObj['title4_1']) {
							//配置子表的新增和删除
							changeEditor(
								'title4_1',
								'checkerStr',
								{
									showDel: !_that.checkedit,
									showAdd: !_that.checkedit
								},
								null,
								mainData
							);
						}
						
						var reviewerList = _that._.cloneDeep(objData.reviewerList);
						_that.subEditorObj['title5'].workList = reviewerList;
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, 'reviewerStr', JSON.stringify(reviewerList));
						_that.$set(_that.formdata, 'reviewerStr', JSON.stringify(reviewerList));
						delete objData.reviewerList;
						for (var i in objData) {
							if (fieldArr.includes(i)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, objData[i]);
								_that.$set(_that.formdata, i, objData[i]);
							}
						}
						if (_that.subEditorObj['title5']) {
							//配置子表的新增和删除
							changeEditor(
								'title5',
								'reviewerStr',
								{
									showDel: !_that.checkedit,
									showAdd: !_that.checkedit
								},
								null,
								mainData
							);
						}
						
						
						
						//修改时初始化下拉框的值
						_that.$nextTick(function() {
							if (this.$refs.autoform) {
								this.$refs.autoform.initSelectIndex('topinfo', 'zposId');
								this.$refs.autoform.initSelectIndex('title1', 'userPostId');
								this.$refs.autoform.formdata.userId = objData.userId;
								this.$refs.autoform.formdata.userName = objData.userName;
								this.$refs.autoform.initSelectIndex('title2', 'importanceId');
								this.$refs.autoform.initSelectIndex('title2', 'orderTypeId');
								this.$refs.autoform.initSelectIndex('title2', 'saleTypeId');
								this.$refs.autoform.initSelectIndex('title2', 'trafficTypeId');
							}
						});
					}else{
						// _that.$nextTick(function() {
						// 	if (this.$refs.autoform) {
						// 		if(res[0].length == 1){
						// 			this.$refs.autoform.initSelectIndex('topinfo', 'zposId',res[0][0].id);
						// 		}
						// 	}
						// });
					}	
				},
				options
			);
		}
	},
	onLoad(options) {
		this.initLoad(options);
	},
	onUnload(){
		//#ifdef APP-PLUS
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				uni.removeStorage({
					key: getWorkCacheKey(fieldId)
				});
			}
		}
		//#endif
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				let editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						if (fieldId == 'title3') {
							var valObj = _that.costSumVal(res.data);
							// //赋值合计数据
							// _that.$refs.autoform.formdata.expectTotalPriceNum = valObj.expectTotalPriceNum;
							// _that.$refs.autoform.formdata.totalPriceNum = valObj.totalPriceNum;
							//拼接合计显示提示
							var showTitle = '';
							if (valObj.expectTotalPriceNum) {
								var fieldName = getEditorTitle('title3', 'expectTotalPriceNum', _that.subConfigData[fieldId]);
								showTitle = fieldName + '合计：' + valObj.expectTotalPriceNum;
							}
							if (showTitle) {
								showTitle + ',';
							}
							if (valObj.totalPriceNum) {
								var fieldName = getEditorTitle('title3', 'totalPriceNum', _that.subConfigData[fieldId]);
								showTitle = showTitle + fieldName + '合计：' + valObj.totalPriceNum;
							}
							_that.$refs.autoform.changeConfigAttr('title3', 'subTitle', showTitle);
						}
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			uni.getStorage({
				key: _that.userCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						_that.loadDataByUserPost(d);
						uni.removeStorage({
							key: _that.userCacheKey
						});
					}
				}
			});
		}
	}
};
</script>
<style></style>
